package com.huonan.component.kafka;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class KafkaProducer {

    public static final String TOPIC = "test_send_msg";

    private final KafkaTemplate<String, String> kafkaTemplate;

    public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String message) {
        String now = DateUtil.now();
        String msg = now + "-" + RandomUtil.randomInt(100);
//        kafkaTemplate.send(TOPIC, msg);
        String key = RandomUtil.randomString("te", 1);
        log.info("key:{}", key);
        kafkaTemplate.send(TOPIC, key, msg);

    }
}